//
//  EFBLackChartsTableViewController.m
//  Pods
//
//  Created by E.F.B on 16/9/1.
//
//

#import "EFBLackChartsTableViewController.h"

@interface EFBLackChartsTableViewController ()

@property (nonatomic,retain) NSArray * sections;
@property (nonatomic,retain) NSDictionary * dataDictionary;

@end

@implementation EFBLackChartsTableViewController

- (id)initWithCharts:(NSMutableDictionary *)charts
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        _dataDictionary = [[NSDictionary alloc] initWithDictionary:charts];
        NSArray *tp = [[charts allKeys] sortedArrayUsingSelector:@selector(compare:)];
        _sections = [[NSArray alloc]initWithArray:tp];
    }
    return self;
}

/*
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"详细信息";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sections;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sections count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *airport = [self tableView:tableView titleForHeaderInSection:section];
    airport = [airport isEqualToString:@""] ? @"NAIP" : airport;
    NSArray *data = [self.dataDictionary objectForKey:airport];
    return [data count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *airport = [self.sections objectAtIndex:section];
    return ([airport isEqualToString:@"NAIP"] ? @"" : airport);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"lackChartCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }
    NSString *airport = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    airport = [airport isEqualToString:@""] ? @"NAIP" : airport;
    NSArray *data = [self.dataDictionary objectForKey:airport];
    NSArray *array = [data objectAtIndex:indexPath.row];
    cell.textLabel.text = [array firstObject];
    cell.detailTextLabel.text = [array lastObject];
    return cell;
}

@end
